package entity;

import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang.builder.HashCodeBuilder;

import util.DateUtil;

/**
 * Course entity.
 * 
 * @author mohameds.2012, sheryl.tay.2012, huangjm.2012, jaslin.wee.2012
 * 
 */
public class Course {
	private String courseCode;
	private String title;
	private String description;
	private String school;
	private Date examDate;
	private Date examStartTime;
	private Date examEndTime;

	/**
	 * Stores prerequisites for the course if available.
	 */
	private ArrayList<Course> preRequisites;

	public Course(String courseCode, String title, String description,
			String school, Date examDate, Date examStartTime, Date examEndTime) {
		this.courseCode = courseCode;
		this.title = title;
		this.description = description;
		this.school = school;
		this.examDate = examDate;
		this.examStartTime = examStartTime;
		this.examEndTime = examEndTime;
	}

	public String getCourseCode() {
		return courseCode;
	}

	public String getTitle() {
		return title;
	}

	public String getDescription() {
		return description;
	}

	public String getSchool() {
		return school;
	}

	public ArrayList<Course> getPreRequisites() {
		if (preRequisites == null) {
			throw new UnsupportedOperationException("Prerequisites for "
					+ courseCode + " is not set");
		}
		return preRequisites;
	}

	public void setPreRequisite(ArrayList<Course> preRequisites) {
		this.preRequisites = preRequisites;
	}

	public Date getExamStart() {
		return DateUtil.combineDateAndTime(examDate, examStartTime);
	}

	public Date getExamEnd() {
		return DateUtil.combineDateAndTime(examDate, examEndTime);
	}

	public String getExamDate(String pattern) {
		return DateUtil.formatDate(examDate, pattern);
	}

	public String getExamStartTime(String pattern) {
		return DateUtil.formatDate(examStartTime, pattern);
	}

	public String getExamEndTime(String pattern) {
		return DateUtil.formatDate(examEndTime, pattern);
	}

	@Override
	public String toString() {
		return "Course [courseCode=" + courseCode + ", title=" + title
				+ ", description=" + description + ", school=" + school
				+ ", examDate=" + examDate + ", examStartTime=" + examStartTime
				+ ", examEndTime=" + examEndTime + ", preRequisites="
				+ preRequisites + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Course) {
			Course anotherObj = (Course) obj;
			return courseCode.equals(anotherObj.getCourseCode());
		}
		return false;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 31).append(courseCode).toHashCode();
	}
}
